package com.tboss.template.system.controller;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.tboss.template.system.dto.CodeTypeDto;
import com.tboss.template.system.service.CodeTypeService;

@Controller
@RequestMapping(value = "/template/system/codeType")
public class CodeTypeController {
	
	@Autowired private CodeTypeService cdTpService;
	
	@RequestMapping(value = {"/_list{reqPage}", "/_list/{reqPage}", "/_listConts/{reqPage}"}, 
			method = {RequestMethod.POST, RequestMethod.GET})//
	public String cdTpList(@PathVariable("reqPage") Integer reqPage, @ModelAttribute CodeTypeDto dto,
			Model model, HttpServletRequest req){

		String rtnUrl = req.getServletPath().substring(1);		
		if(reqPage != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"));
		
		this.cdTpService.setModelCodeTypeList(reqPage, model, dto);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_regForm"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String cdTpRegForm(Model model, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		
		this.cdTpService.getCodeTypeRegForm(model);

		return rtnUrl+"Ajax";
	}

	@RequestMapping(value = {"/_regFormSave"}, method = {RequestMethod.POST})//
	public Model cdTpRegFormSave(@ModelAttribute CodeTypeDto dto, Model model, HttpServletRequest req){
		this.cdTpService.addCodeTypeInfo(dto);

		return model;
	}
	
	@RequestMapping(value = {"/_regFormRevise/{cdTpId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String cdTpRegFormRevise(Model model, @PathVariable("cdTpId") String cdTpId, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		if(cdTpId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("Revise/"+cdTpId));
		
		this.cdTpService.getCodeTypeRegForm(model, cdTpId);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_regFormReviseSave"}, method = {RequestMethod.POST})//
	public Model cdTpRegFormReviseSave(@ModelAttribute CodeTypeDto dto, Model model, HttpServletRequest req){
		this.cdTpService.updateCodeTypeInfo(dto);
		
		return model;
	}
	
	@RequestMapping(value = {"/_detail/{cdTpId}"}, method = {RequestMethod.POST, RequestMethod.GET})//
	public String cdTpInfoDetail(Model model, @PathVariable("cdTpId") String cdTpId, HttpServletRequest req){
		String rtnUrl = req.getServletPath().substring(1);
		if(cdTpId != null) rtnUrl = rtnUrl.substring(0, rtnUrl.lastIndexOf("/"+cdTpId));
		
		this.cdTpService.getCodeTypeRegForm(model, cdTpId);
		
		return rtnUrl+"Ajax";
	}
	
	@RequestMapping(value = {"/_deleteCodeType"}, method = {RequestMethod.POST})//
	public Model deleteCodeTypeInfo(@ModelAttribute CodeTypeDto dto, Model model, HttpServletRequest req){
		this.cdTpService.deleteCodeTypeInfo(dto);
		
		return model;
	}

}
